# -*- coding: utf-8 -*-

from osv import fields,osv
from osv.orm import browse_record
import tools
from functools import partial
import pytz
import pooler
from tools.translate import _
from service import security
import netsvc
import datetime

def get_readonly(user, type='timetable'):
    res = True
    for profile in user.profiles_ids:
        if profile.name in (u'Sinh viên', u'Giảng viên'):
            res = False
            break
    return res

class aas_plan_timetable(osv.TransientModel):
    _name = 'aas.plan.timetable'
    _columns = {
        'courses_ids': fields.many2many('aas.course.course', 'plan_timetable_course', 'plan_id', 'course_id', string='Chi tiết thời khóa biểu', context={'mode': 2}),
        'readonly': fields.boolean('', readonly=True),
    }
    _defaults = {  
        'readonly': lambda *a: True,
    }
    
    def default_get(self, cr, uid, fields, context=False):
        res = super(aas_plan_timetable, self).default_get(cr, uid, fields, context=context)
        id = context.get('active_id', False)
        self.list_id = False
        if id:
            list_obj = self.pool.get('aas.course.list')
            course_obj = self.pool.get('aas.course.course')
            list = list_obj.browse(cr, uid, id, context=context)
            ids = []
            for course in list.courses:
                ids.append(course.id)
            res.update({'courses_ids': ids})
            
            if ids:
                sql = '''
                    SELECT list_id
                    FROM aas_course_course
                    WHERE id = %s
                '''
                cr.execute(sql, (ids[0],))
                if cr.rowcount > 0:
                    self.list_id = cr.fetchone()[0]
        user_obj = self.pool.get('res.users')
        user = user_obj.browse(cr, uid, uid, context=context)
        res.update({'readonly': get_readonly(user)})
        return res
    
    def print_report(self, cr, uid, ids, context=False):
        datas = {}
        if not self.list_id:
            raise osv.except_osv(_('Thông báo!'), _("Thời khóa biểu trống, không thể tải bản in!"))
        datas['id'] = self.list_id
        datas['ids'] = [self.list_id]
        datas['model'] = 'aas.course.list'
        return {
            'type': 'ir.actions.report.xml', 
            'report_name': 'aas_timetable_report', 
            'datas': datas
        }
        
aas_plan_timetable()


class aas_plan_exam_schedule(osv.TransientModel):
    _name = 'aas.plan.exam.schedule'
    _columns = {
        'courses_ids': fields.many2many('aas.course.course', 'plan_exam_schedule_course', 'plan_id', 'course_id', string='Chi tiết lịch thi', context={'mode': 1}),
        'readonly': fields.boolean('', readonly=True),
    }
    _defaults = {  
        'readonly': lambda *a: True,
    }
    
    def default_get(self, cr, uid, fields, context=False):
        res = super(aas_plan_exam_schedule, self).default_get(cr, uid, fields, context=context)
        id = context.get('active_id', False)
        self.list_id = False
        if id:
            list_obj = self.pool.get('aas.course.list')
            course_obj = self.pool.get('aas.course.course')
            list = list_obj.browse(cr, uid, id, context=context)
            ids = []
            for course in list.courses:
                ids.append(course.id)
            res.update({'courses_ids': ids})
            
            if ids:
                sql = '''
                    SELECT list_id
                    FROM aas_course_course
                    WHERE id = %s
                '''
                cr.execute(sql, (ids[0],))
                if cr.rowcount > 0:
                    self.list_id = cr.fetchone()[0]
        user_obj = self.pool.get('res.users')
        user = user_obj.browse(cr, uid, uid, context=context)
        res.update({'readonly': get_readonly(user)})
        return res
    
    def print_report(self, cr, uid, ids, context=False):
        datas = {}
        if not self.list_id:
            raise osv.except_osv(_('Thông báo!'), _("Lịch thi trống, không thể tải bản in!"))
        datas['id'] = self.list_id
        datas['ids'] = [self.list_id]
        datas['model'] = 'aas.course.list'
        return {
            'type': 'ir.actions.report.xml', 
            'report_name': 'aas_exam_schedule_report', 
            'datas': datas
        }
        
aas_plan_exam_schedule()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
